package com.android.aishangauction;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.aishangauction.service.WebSocketService;
import com.android.aishangauction.service.WebSocketService1;
import com.android.aishangauction.service.WebSocketService2;
import com.android.aishangauction.service.WebSocketService3;
import com.android.aishangauction.utils.ConstantsUrl;
import com.android.aishangauction.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 引导页
 */

public class GuideActivity extends AppCompatActivity implements View.OnClickListener {

    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.indicator)
    LinearLayout indicator;

    private List<View> viewList;
    private ImageView[] indicatorImgs;
    private int[] imgResArr = new int[]{R.mipmap.guide_1, R.mipmap.guide_2};
    private boolean isExit;

    public static void start(Context context) {
        Intent intent = new Intent(context, GuideActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_guide);
        ButterKnife.bind(this);
        initData();
        initView();
        super.onCreate(savedInstanceState);
    }

    public void initData() {
        //是否显示底部圆点
        indicator.setVisibility(View.GONE);
        int length = imgResArr.length;
        viewList = new ArrayList<View>(length);
        indicatorImgs = new ImageView[length];
        for (int i = 0; i < length; i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.activity_guide_item, null);
            view.setBackgroundResource(R.color.white);
            ImageView imgView = (ImageView) view.findViewById(R.id.guide_image);
            imgView.setBackgroundResource(imgResArr[i]);
            viewList.add(view);
            imgView.setOnClickListener(this);

            indicatorImgs[i] = new ImageView(this);
            if (i == 0) {
                indicatorImgs[i].setBackgroundResource(R.mipmap.ic_indicators_selected);
            } else {
                indicatorImgs[i].setBackgroundResource(R.mipmap.ic_indicators_default);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-2, -2);
                layoutParams.setMargins(20, 0, 0, 0);
                indicatorImgs[i].setLayoutParams(layoutParams);
            }
            indicator.addView(indicatorImgs[i]);

        }
    }

    public void initView() {
        viewpager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(viewList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(viewList.get(position));
                return viewList.get(position);
            }
        });

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setIndicator(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void setIndicator(int targetIndex) {
        for (int i = 0; i < indicatorImgs.length; i++) {
            indicatorImgs[i].setBackgroundResource(R.mipmap.ic_indicators_selected);
            if (targetIndex != i) {
                indicatorImgs[i].setBackgroundResource(R.mipmap.ic_indicators_default);
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guide_image:
                if (viewpager.getCurrentItem() == (imgResArr.length - 1)) {
                    SharedPreferences myPref = getSharedPreferences(ConstantsUrl.SHAREDPREFERENCES_NAME, 0);
                    SharedPreferences.Editor editor = myPref.edit();
                    editor.putBoolean(ConstantsUrl.CACHE_HOME_GUIDE, true);
                    editor.commit();
                    startActivity(new Intent(this, MainActivity.class));
                    finish();
                }
                break;
        }
    }

    Handler mHandler = new Handler();

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (isExit) {
                SharedPreferences sp = getSharedPreferences("time", 0);
                SharedPreferences.Editor edit = sp.edit();
                edit.clear();
                edit.commit();
                destroyService();
                System.exit(0);
            } else {
                ObjectUtils.toast(this, "再按一次就可以退出了");
                isExit = true;
            }
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    isExit = false;
                }
            }, 2000);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void destroyService(){
        Intent serviceIntent = new Intent(this, WebSocketService.class);
        Intent serviceIntent1 = new Intent(this, WebSocketService1.class);
        Intent serviceIntent2 = new Intent(this, WebSocketService2.class);
        Intent serviceIntent3 = new Intent(this, WebSocketService3.class);
        stopService(serviceIntent);
        stopService(serviceIntent1);
        stopService(serviceIntent2);
        stopService(serviceIntent3);
    }
}
